<%-- 
    Document   : SendMail
    Created on : May 13, 2011, 2:53:26 AM
    Author     : Stephen Moulton
--%>
<%@page import="javax.mail.internet.InternetAddress"%>
<%@page import="javax.mail.internet.MimeMessage"%>
<%@page import="java.util.Properties"%>
<%@page import="javax.mail.Transport"%>
<%@page import="javax.mail.Session"%>
<%@page import="javax.mail.Message"%>

<%
    String host="", user="", pass="";
    host = "smtp.gmail.com";
    user = "group5.lancs@gmail.com";
    pass = "group5lancs"; //Your gmail password
    String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
    String to = "group5.lancs@gamil.com"; // out going email id
    String from = request.getParameter("email"); //Email id of the recipient
    String subject = "Message for group 5 website: " + request.getParameter("name") + " " +
            request.getParameter("message");; 
    String messageText = request.getParameter("message");
    boolean sessionDebug = true;
    Properties props = System.getProperties();
    props.put("mail.host", host);
    props.put("mail.transport.protocol.", "smtp");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.", "true");
    props.put("mail.smtp.port", "465");
    props.put("mail.smtp.socketFactory.fallback", "false");
    props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
    Session mailSession = Session.getDefaultInstance(props, null);
    mailSession.setDebug(sessionDebug);
    Message msg = new MimeMessage(mailSession);
    msg.setFrom(new InternetAddress(from));
    InternetAddress[] address = {new InternetAddress(to)};
    msg.setRecipients(Message.RecipientType.TO, address);
    msg.setSubject(subject);
    msg.setContent(messageText, "text/html"); // use setText if you want to send text
    Transport transport = mailSession.getTransport("smtp");
    transport.connect(host, user, pass);
    try {
        transport.sendMessage(msg, msg.getAllRecipients());
        response.sendRedirect("contact.jsp?sent=0");
    }
    catch (Exception err) {
        response.sendRedirect("contact.jsp?sent=1");
    }
    transport.close();
%>